import java.util.Scanner;

/**
 * @version 1.8
 * @Author Djx_hmbb
 * @Date 2023/8/14 8:40
 * @注释:内存可见性问题 volatile[易变的]
 */
class MyCounter{
    public volatile int flag = 0;
}
public class ThreadDemo13 {
    public static void main(String[] args) {
        MyCounter myCounter = new MyCounter();
        Thread t1 = new Thread(()->{
            while(myCounter.flag == 0){

            }
            System.out.println("t1执行结束~");
        });

        Thread t2 = new Thread(()->{
            Scanner s = new Scanner(System.in);
            System.out.println("请输入flag:");
            myCounter.flag = s.nextInt();
        });
        t1.start();
        t2.start();
    }
}
